<template>
  <span>
    <el-button @click="handleClick"
               type="text">{{title}}</el-button>
    <quote-matching-dialog v-if="visible"
                           :visible.sync="visible"
                           :preset="preset" />
  </span>
</template>

<script>
import axios from 'axios';
import { quotation, transport } from "@/api/index";
import QuoteMatchingDialog from "@/components/dialog/tableFrame/QuoteMatchingDialog.vue"
export default {
  name: "WaybillQuotaionAction",
  inject: ["$table"],
  components: { QuoteMatchingDialog },
  props: {
    record: {
      type: Object,
      default: () => { }
    },
    isMatch: [String, Number],
    isRefresh: [String, Number],
    // 匹配弹窗相关配置
    dialogUrl: {
      type: String,
      default: null
    },
    // 按钮名称
    title: String
  },
  data () {
    return {
      visible: false,
      preset: {},
      transform: {
        quotationId: "quotationId",
        subjectId: "subjectId",
        waybillId: "waybillId",
        status: "status",
        offLoad: "offLoad",
        onLoad: "onLoad",
        pick: "pick",
        deliver: "deliver",
        insure: "insure"
      }
    };
  },
  computed: {
    tips () {
      return {
        3: "运单有关单待办需要处理，暂不能操作",
        5: "运单已对账，不能操作",
        6: "运单已结账，不能操作",
        7: "财务已将运单加入到对账单，不能操作，请联系财务处理"
      }
    }
  },
  methods: {
    async quoteCheckValidate () {
      let response = await axios.get(`/quotation/check/${this.record?.subjectId}`);
      if (response.code === 0) {
        if (response.data.validate) {
          this.quoteValidate();
        } else if ([3, 7].includes(response.data?.flag)) {
          return this.$message({ type: 'warning', message: this.tips[response.data.flag] })
        } else if ([5, 6].includes(response.data.flag)) {
          this.$confirm(
            this.tips[response.data?.flag],
            "提示",
            {
              confirmButtonText: "我知道了",
              type: "warning",
              showClose: false,
              showCancelButton: false,
              closeOnClickModal: false
            }
          ).then(() => {
            this.closeQuotaion(response.data?.flag);
          });
        }
      } else {
        this.$message({ type: "error", message: response.message });
      }
    },
    // 关闭报价单
    async closeQuotaion (closeType) {
      await transport.closeWaybillQuote({ waybillId: this.record?.waybillId });
      // 刷列表 
      this.$table.search();
    },
    // 报价前置校验
    async quoteValidate () {
      let response = await axios.get(`/quotation/validate/${this.record?.subjectId}`);
      if (response.code === 0) {
        if (response.data.validate) {
          if (this.isMatch || this.isRefresh) {
            this.quoteMatchValidate();
          } else {
            this.onJump(1);
          }
        } else if (response.data.flag === 1) {
          // flag:1 已经有人发起了报价
          this.$confirm(`已向您发起了报价，是否去审核?`, "提示", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(() => {
              this.onJump(2, response.data?.quotationId);
            });
        } else if (response.data.flag === 2) {
          // flag:2 我已经发起了报价
          this.$confirm("不能重复报价，请刷新页面?", "提示", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(() => {
              // 刷列表 
              this.$table.search();
            });
        } else if (response.data.flag === 3) {
          return this.$message({ type: 'warning', message: `运单有关单待办需要处理，暂不能操作` })
        }
      } else {
        this.$message({ type: "error", message: response.message });
      }
    },
    // 报价是否能够匹配合约前置校验
    async quoteMatchValidate () {
      let response = await axios.post(this.isRefresh ? `/quotation/template/refresh` : `/quotation/template/match`, { subjectId: this.record?.subjectId });
      if (this.isRefresh) {
        // 价格重算
        if (response.data.refresh) {
          this.preset = {
            param: this.transformData({ ...this.record }, this.transform),
            childName: {
              ...response.data,           //合约匹配到的数据
            },
            url: this.dialogUrl
          }
          this.visible = true;
        } else {
          return this.$message({ type: "warning", message: "未匹配到新的合约" });
        }
      } else {
        // 合约匹配
        if (response.data.match) {
          this.preset = {
            param: this.transformData({ ...this.record }, this.transform),
            childName: {
              ...response.data,           //合约匹配到的数据
            },
            url: this.dialogUrl
          }
          this.visible = true;
        } else {
          this.onJump(1);
        }
      }
    },
    transformData (data, transform) {
      let target = {}
      for (let [name, value] of Object.entries(transform)) {
        target[name] = data[value]
      }
      return target
    },
    onJump (type, id) {
      this.$router.push({
        name: "quoteDetail",
        query: {
          quotationId: id || this.record?.quotationId,
          waybillId: this.record?.waybillId,
          subjectId: this.record?.subjectId,
          status: type === 1 ? this.record?.status : "11",
          surcharge: this.record?.surcharge
        }
      });
    },
    handleClick () {
      if (this.record.combineContractNo) {
        this.$router.push({
          name: "oneBillQuote",
          query: {
            combineId: this.record.combineId,
            subjectId: this.record?.subjectId,
            surcharge: false,
            status: "10"
          }
        });
      } else {
        this.quoteCheckValidate();
      }
      // this.$emit("click");
    }
  }
}
</script>
<style scoped lang="scss">
.arrow {
  color: var(--prev-color-text-regular);
  font-size: 13px;
  padding: 0;
}
</style>